import { message } from 'antd';
import { history } from 'umi';
import {
    removeRole,
    getRolelist,
    getRoleDetail,
    getRoleIdlist,
    removeRoleId,
    getMenuTree,
    addRoles,
    getDepartmentTree
} from '@/services/organization';
import getemployeemenu from '@/services/accessService';
import authHelper from '@/utils/auth/helper';

export default {
    namespace: 'role',
    state: {
        roleList: [],
        rolePagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        order: '',
        roleIdList: [],
        roleDetail: {}, // 角色详情
        department: [], // 获取部门树
        menuList: [], // 菜单生成树
        defaultDepartment: [] // 表单初始部门数据
    },

    effects: {
        // 角色删除
        *roleDelete({ payload, success }, { call }) {
            const { rid } = payload;
            const result = yield call(removeRole, rid);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success(result.data);
            }
            if (result.code === 60014) {
                message.success(result.msg);
            }
        },
        // 角色详情
        *roleDetail({ payload, success }, { put, call }) {
            const { id } = payload;
            const result = yield call(getRoleDetail, id);
            if (result.code === 10000) {
                yield put({
                    type: 'saveRole',
                    payload: {
                        roleDetail: result.data,
                        defaultDepartment: result.data.dataDepartment
                    }
                });
                // eslint-disable-next-line no-unused-expressions
                success && success(result.data);
            }
        },
        // 角色列表展示
        *fetchRoleList({ payload }, { call, put }) {
            const result = yield call(getRolelist, payload);

            if (result && result.code === 10000) {
                yield put({
                    type: 'saveRole',
                    payload: {
                        roleList: result.data.rows,
                        rolePagination: {
                            pageSize: result.data.pageSize,
                            current: result.data.pageNo,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 角色成员列表展示
        *fetchRoleIdList({ payload }, { call, put }) {
            const { roleId, companyId } = payload;
            const result = yield call(getRoleIdlist, roleId, companyId);
            if (result && result.code === 10000) {
                yield put({
                    type: 'saveRole',
                    payload: {
                        roleIdList: result.data
                    }
                });
            }
        },
        // 角色成员删除
        *roleIdDelete({ payload }, { put, call, select }) {
            const { rid, employeeId, companyId } = payload;
            const result = yield call(removeRoleId, rid, employeeId);
            if (result.code === 10000) {
                message.success(result.msg);
                yield put({
                    type: 'fetchRoleIdList',
                    payload: {
                        roleId: rid,
                        companyId
                    }
                });
                const stateDate = yield select(state => state.role);
                yield put({
                    type: 'fetchRoleList',
                    payload: {
                        companyId: payload.roleCompanyId,
                        roleName: payload.roleNameSearch || '',
                        pageNo: stateDate.rolePagination.current,
                        pageSize: stateDate.rolePagination.pageSize,
                        orderBy: stateDate.order
                    }
                });
            }
        },
        // 菜单生成树
        *fetchMenuList(_, { call, put }) {
            const result = yield call(getMenuTree);
            if (result.code === 10000) {
                yield put({
                    type: 'saveRole',
                    payload: {
                        menuList: result.data
                    }
                });
            }
        },
        // 部门展示
        *fetchDepartmentList({ payload }, { call, put }) {
            const result = yield call(getDepartmentTree, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'saveRole',
                    payload: {
                        department: result.data
                    }
                });
            }
        },
        // 角色添加
        *testAddRole({ payload }, { call, put }) {
            // const { companyId } = payload;
            const result = yield call(addRoles, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                yield put({
                    type: 'saveRole',
                    payload: {
                        roleDetail: []
                    }
                });
                history.push('/setting/role');
            }
            if (result.code === 60005) {
                message.success(result.msg);
            }
        },
        // 角色菜单
        *roleGetMenu(_, { call }) {
            const menu = yield call(getemployeemenu);
            if (menu.code === 10000) {
                authHelper.filterRouters = menu.data;
            }
        }
    },
    reducers: {
        saveRole(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
